package imagekeeper.data;

import java.sql.Connection;
import java.sql.Types;
import java.sql.SQLException;
import imagekeeper.data.mapping.ImageMapper;
import imagekeeper.dmi.Image;
import imagekeeper.dmi.User;


class SqlStatementCreateImage extends SqlStatement {
    private static final String ParamTitle = "title";
    private static final String ParamDescription = "description";
    private static final String ParamOwnerId = "ownerId";
    private static final String ParamPrivacy = "privacy";
    private static final String ParamUrl = "url";
    private static final String ParamRemovingLink = "removingLink";
    private static final String ParamImageId = "imageId";
    
    public SqlStatementCreateImage(Connection con) throws SQLException {
        statement = con.prepareCall("{call " + ImageMapper.SpCreateImage + "(?,?,?,?,?,?,?)}");
        statement.registerOutParameter(ParamImageId, Types.INTEGER);
    }
    
    public SqlStatementCreateImage(Connection con, Image newImg) throws SQLException {
        this(con);
        setTitle(newImg.getTitle());
        setDescription(newImg.getDescription());
        setOwnerId(newImg.getOwner());
        setPrivacy(newImg.getPrivacy());
        setUrl(newImg.getUrl());
        setRemovingLink(newImg.getRemovingLink());
    }
    
    public final int getImageId() throws SQLException {
        return statement.getInt(ParamImageId);
    }
    
    public final void setTitle(String title) throws SQLException {
        setParameter(ParamTitle, title, Types.VARCHAR);
    }
    
    public final void setDescription(String descr) throws SQLException {
        setParameter(ParamDescription, descr, Types.VARCHAR);
    }
    
    public final void setOwnerId(User owner) throws SQLException {
        if (owner != null)
            statement.setInt(ParamOwnerId, owner.getId());
        else
            statement.setNull(ParamOwnerId, Types.INTEGER);
    }
    
    public final void setPrivacy(boolean privacy) throws SQLException {
        statement.setBoolean(ParamPrivacy, privacy);
    }
    
    public final void setUrl(String url) throws SQLException {
        statement.setString(ParamUrl, url);
    }
    
    public final void setRemovingLink(String link) throws SQLException {
        setParameter(ParamRemovingLink, link, Types.VARCHAR);
    }
}
